﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Drawing.Text;

namespace Walson.Form.FlightInstrument
{

    /// <summary>
    /// 时钟和计时器
    /// </summary>
    public class Watch : Control
    {
        #region 必要的东西
        /// <summary> 
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary> 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // VerticalSpeedInidicator
            // 
            this.DoubleBuffered = true;
            this.Name = "HomeInidicator";
            this.Size = new System.Drawing.Size(90, 90);

            this.ResumeLayout(false);

        }

        #endregion
        #endregion

        private int _InnerTextSize = 10;
        public int InnerTextSize
        {
            get
            {
                return _InnerTextSize;
            }
            set
            {
                if(value!=_InnerTextSize)
                {
                    _InnerTextSize=value;
                    this.Invalidate();
                }
            }
        }

        private Font _Font = new Font("Terminal", 12);
        public Font Font
        {
            get
            {
                return _Font;
            }
            set
            {
                if (_Font != value)
                {
                    _Font = value;
                    this.Invalidate();
                }
            }
        }

        private int _UpperTextBoxOffsetY = 0;
        public int UpperTextBoxOffsetY
        {
            get
            {
                return _UpperTextBoxOffsetY;
            }
            set
            {
                if (_UpperTextBoxOffsetY != value)
                {
                    _UpperTextBoxOffsetY = value;
                    this.Invalidate();
                }
            }
        }

        private int _LowerTextBoxOffsetY = 0;
        public int LowerTextBoxOffsetY
        {
            get
            {
                return _LowerTextBoxOffsetY;
            }
            set
            {
                if (_LowerTextBoxOffsetY != value)
                {
                    _LowerTextBoxOffsetY = value;
                    this.Invalidate();
                }
            }
        }

        private Timer _IntervalTimer;
        private Timer _IntervalRefreshTimer;
        private int _ElapsedSecond = 0;
        private int _IntervalMinute = 0;
        private Pen _Pen;
        //private bool _TimerStarted = false;
        private bool _TimerMode = false;
        private bool _ShowDot = false;

        private bool _InitTimer = false;

        /// <summary>
        /// 启动计时器
        /// </summary>
        public void StartTimer()
        {
            if (_TimerMode == false)
                return;

            _ElapsedSecond = 0;
            _IntervalMinute = 0;
            //_TimerStarted = true;
            _TimerMode = true;
            _IntervalTimer.Enabled = true;
            this.Invalidate();
        }

        /// <summary>
        /// 停止计时器
        /// </summary>
        public void StopTimer()
        {
            _IntervalTimer.Enabled = false;
            //_TimerStarted = false;
            this.Invalidate();
        }

        /// <summary>
        /// 复位定时器
        /// </summary>
        public void ResetTimer()
        {
            _ElapsedSecond = 0;
            _IntervalMinute = 0;
            _TimerMode = false;
            //_IntervalTimer.Enabled = false;
            //_TimerStarted = false;
            this.Invalidate();
        }

        /// <summary>
        /// 时钟和计时器
        /// </summary>
        public Watch()
        {
            InitializeComponent();
            _IntervalTimer = new Timer();
            _IntervalTimer.Interval = 1000;
            _IntervalTimer.Tick += delegate
            {
                //if (_TimerStarted)
                _ElapsedSecond++;
                if (_ElapsedSecond == 60)
                {
                    _ElapsedSecond = 0;
                    _IntervalMinute++;
                }
                this.Invalidate();
            };
            _IntervalTimer.Enabled = false;

            _IntervalRefreshTimer = new Timer();
            _IntervalRefreshTimer.Interval = 500;
            _IntervalRefreshTimer.Tick += delegate
            {
                _ShowDot = !_ShowDot;
                this.Invalidate();
            };
            _IntervalRefreshTimer.Enabled = true;


            _Pen = new Pen(Color.White, 2f);

            this.DoubleBuffered = true;

        }

        public enum Mode
        {
            Watch,
            Timer
        }

        public void SetMode(Mode mode)
        {
            if (mode == Mode.Timer)
            {
                _InitTimer = true;
                _TimerMode = true;
            }
            else
            {
                _InitTimer = false;
                _TimerMode = false;
                //StopTimer();
            }

            this.Invalidate();
        }

        /// <summary>
        /// 绘制控件
        /// </summary>
        /// <param name="e">参数</param>
        protected override void OnPaint(PaintEventArgs e)
        {
            int min = Math.Min(this.Width, this.Height);
            int max = Math.Max(this.Width, this.Height);
            int r = -min / 2 + 30;
            float scaleFactorX = (float)this.Width / (float)max;
            float scaleFactorY = (float)this.Height / (float)max;
            SizeF size;
            //Font _Font = new Font("Terminal", 12);
            using (Bitmap bmp = new Bitmap(this.Width, this.Height))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    g.TextRenderingHint = TextRenderingHint.AntiAlias;
                    g.TranslateTransform(this.Width / 2f, this.Height / 2f);

                    g.Clear(Color.FromArgb(55, 55, 63));

                    PointF[] line = new PointF[2];
                    for (int i = 0; i < 60; i++)
                    {
                        line[0] = new PointF(0, r - 10);

                        if (i % 5 == 0)
                        {
                            line[1] = new PointF(0, r - 25);
                            if (i % 10 == 0)
                            {
                                int angle = i;
                                size = g.MeasureString(angle.ToString(), _Font);
                                g.DrawString(angle.ToString(), _Font, Brushes.White, -size.Width / 2, r);
                            }
                        }
                        else
                        {
                            line[1] = new PointF(0, r - 17);
                        }
                        g.DrawLine(_Pen, line[0], line[1]);

                        g.RotateTransform(6);
                    }

                    g.ResetTransform();


                    g.TranslateTransform(this.Width / 2f, this.Height / 2f);
                    string now;
                    int hour, minute, second;
                    hour = DateTime.Now.Hour;
                    minute = DateTime.Now.Minute;
                    if (_InitTimer == false)
                        second = DateTime.Now.Second;
                    else
                        second = 0;

                    if (_ShowDot)
                        now = DateTime.Now.Hour.ToString("D2") + ":" + DateTime.Now.Minute.ToString("D2");
                    else
                        now = DateTime.Now.Hour.ToString("D2") + " " + DateTime.Now.Minute.ToString("D2");
                    
                    Font font = new Font(new Walson.Drawing.ResourceFont().GetEmbeddedFont(Assembly.GetExecutingAssembly().GetName().Name.ToString(), "SegNumbers.ttf"), _InnerTextSize);
                    size = g.MeasureString(now.ToString(), font);
                    PointF[] region = new PointF[5];
                    region[0] = new PointF(-size.Width / 2f - 5f, -size.Height * 3 + _UpperTextBoxOffsetY);
                    region[1] = new PointF(size.Width / 2f + 5f, -size.Height * 3 + _UpperTextBoxOffsetY);
                    region[2] = new PointF(size.Width / 2f + 5f, -size.Height * 2 + _UpperTextBoxOffsetY);
                    region[3] = new PointF(-size.Width / 2f - 5f, -size.Height * 2 + _UpperTextBoxOffsetY);
                    region[4] = new PointF(-size.Width / 2f - 5f, -size.Height * 3 + _UpperTextBoxOffsetY);
                    g.FillPolygon(new SolidBrush(Color.FromArgb(16, 16, 49)), region);
                    g.DrawLine(Pens.Black, region[0], region[1]);
                    g.DrawLine(Pens.White, region[1], region[2]);
                    g.DrawLine(Pens.White, region[2], region[3]);
                    g.DrawLine(Pens.Black, region[3], region[4]);

                    RectangleF stringRect = new RectangleF(-size.Width / 2, -size.Height * 3 + _UpperTextBoxOffsetY, size.Width, size.Height);
                    StringFormat sf = new StringFormat();
                    sf.Alignment = StringAlignment.Far;
                    g.DrawString("88:88", font, new SolidBrush(Color.FromArgb(32, 255, 255, 255)), stringRect, sf);
                    g.DrawString(now, font, Brushes.White, stringRect, sf);


                    region[0] = new PointF(-size.Width / 2f - 5f, size.Height *2 + _LowerTextBoxOffsetY);
                    region[1] = new PointF(size.Width / 2f + 5f, size.Height * 2 + _LowerTextBoxOffsetY);
                    region[2] = new PointF(size.Width / 2f + 5f, size.Height * 3 + _LowerTextBoxOffsetY);
                    region[3] = new PointF(-size.Width / 2f - 5f, size.Height * 3 + _LowerTextBoxOffsetY);
                    region[4] = new PointF(-size.Width / 2f - 5f, size.Height * 2 + _LowerTextBoxOffsetY);

                    
                    g.FillPolygon(new SolidBrush(Color.FromArgb(16, 16, 49)), region);
                    g.DrawLine(Pens.Black, region[0], region[1]);
                    g.DrawLine(Pens.White, region[1], region[2]);
                    g.DrawLine(Pens.White, region[2], region[3]);
                    g.DrawLine(Pens.Black, region[3], region[4]);
                    size = g.MeasureString("888888".ToString(), font);
                    stringRect = new RectangleF(-size.Width / 2, size.Height * 2 + _LowerTextBoxOffsetY, size.Width, size.Height);
                    sf = new StringFormat();
                    sf.Alignment = StringAlignment.Far;
                    g.DrawString("888888", font, new SolidBrush(Color.FromArgb(32, 255, 255, 255)), stringRect, sf);
                    g.DrawString(_IntervalMinute.ToString(), font, Brushes.White, stringRect, sf);

                    g.ResetTransform();
                    g.TranslateTransform(this.Width / 2f, this.Height / 2f);

                    g.FillEllipse(Brushes.White, -10, -10, 20, 20);
                    g.FillEllipse(Brushes.DarkGray, -5, -5, 10, 10);
                    if (_TimerMode)
                    {
                        g.RotateTransform(_ElapsedSecond * 6f);
                    }
                    else
                        g.RotateTransform(second * 6f);
                    line[0] = new PointF(0, 0);
                    line[1] = new PointF(0, r);

                    PointF[] polygon = new PointF[3];
                    polygon[0] = line[0];
                    polygon[1] = line[1];
                    polygon[2] = new PointF(-6, 20);
                    g.FillPolygon(Brushes.DarkGray, polygon);

                    polygon[2] = new PointF(6, 20);
                    g.FillPolygon(Brushes.Gray, polygon);

                    polygon[1] = new PointF(6, 20);
                    polygon[2] = new PointF(-6, 20);
                    g.FillPolygon(Brushes.LightGray, polygon);

                    // g.DrawLine(Pens.White, line[0], line[1]);

                    g.FillEllipse(Brushes.Gold, -3, -3, 6, 6);

                    g.ResetTransform();
                }
                e.Graphics.DrawImage(bmp, 0, 0);
            }

            // 绘制圆角控件
            Rectangle rect = new Rectangle(0, 0, base.Width, base.Height);
            int radius = min;
            System.Drawing.Drawing2D.GraphicsPath roundRect = new System.Drawing.Drawing2D.GraphicsPath();
            roundRect.AddEllipse(this.Width / 2 - min / 2, this.Height / 2 - min / 2, min - 1, min - 1);
            this.Region = new Region(roundRect);
            base.OnPaint(e);
        }
    }


}
